package com.a01_2021.thousandstimes.instance.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.general.jwt.JwtTokenUtil;
import com.a01_2021.thousandstimes.instance.entity.Note;
import com.a01_2021.thousandstimes.instance.service.INoteService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/note")
public class NoteController {

    @Resource
    JwtTokenUtil jwtTokenUtil;

    @Resource
    INoteService noteService;

    /**
     * 添加便签
     *
     * @param instanceId  实例id
     * @param token       jwt令牌
     * @param noteName    便签名字
     * @param noteContent 便签内容
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('note-addNote')")
    @DS("#header.instanceid")
    @PostMapping("addNote")
    public AjaxResponse addNote(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, String noteName, String noteContent) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        Note note = noteService.addNote(userId, noteName, noteContent);
        return AjaxResponse.success(note);
    }

    /**
     * 获取便签
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('note-getNotes')")
    @DS("#header.instanceid")
    @PostMapping("getNotes")
    public AjaxResponse getNotes(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        List<Note> noteList = noteService.getNotes(userId);
        return AjaxResponse.success(noteList);
    }

    /**
     * 批量添加便签
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @param noteList   便签列表
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('note-addNotes')")
    @DS("#header.instanceid")
    @PostMapping("addNotes")
    public AjaxResponse addNotes(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, @RequestBody List<Note> noteList) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        List<Note> resultList = noteService.addNotes(userId, noteList);
        return AjaxResponse.success(resultList);
    }

    /**
     * 修改便签
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @param note       便签实体
     * @return AjaxResponse
     * @throws CustomException 当便签不存在，用户操作他人便签时抛出
     */
    @PreAuthorize("hasAuthority('note-updateNote')")
    @DS("#header.instanceid")
    @PostMapping("updateNote")
    public AjaxResponse updateNote(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, Note note) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        if (note.getId() == null) {
            return AjaxResponse.error(400, "便签id不可为空");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        Note returnNote = noteService.updateNote(userId, note);
        return AjaxResponse.success(returnNote);
    }
}
